<template>
    <h1 style="text-align: center;">登录页面</h1>
    <link rel="stylesheet" href="./src/assets/DomCss/css/login.css">

    <div class="pure-g content">
	<div class="pure-form pure-u-1">
        <fieldset class="pure-group">
            <input type="text" class="pure-input-1" placeholder="用户名" v-model="Name" required autofocus>
            <input type="password" class="pure-input-1" placeholder="密　码" v-model="Pwd" required>
        </fieldset>
        <button class="pure-button pure-u-1 pure-button-primary" @click="Login" >登　录</button>
	</div>
</div>
<!--BEGIN FOOT-->
<div class="pure-g copyright">
    <div class="pure-u-1"> 2024 2022.netA XTY. </div>
</div>

</template>

<script setup lang="ts">

import { ref } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';
import {useUserStore} from '../../store/index'

const router = useRouter();

const Name = ref('');
const Pwd = ref('');

const useStore = useUserStore();

const Login  = ()=>{
    axios({
        method:'get',
        url:'http://localhost:63517/api/User/Login',
        params:{
            Name:Name.value,
            Pwd:Pwd.value
        }
    })
    .then(res=>{
        console.log(res.data);
        if(res.data.CK_Code==1){
            //登录成功的时候
            alert(res.data.CK_Message);
            //将用户编号保存到仓库中
            useStore.UId = res.data.CK_Ulist[0].U_Id
            //alert(useStore.UId)

            console.log(res.data.CK_Ulist[0].U_Id)
            SearchUid(res.data.CK_Ulist[0].U_Id)
        }
        else if(res.data.CK_Code== 0){
            //账号密码不存在
            alert(res.data.CK_Message);
        }
        else{
            //用户不存在
            alert(res.data.CK_Message);
        }
    })
    .catch(err=>{
        console.log(err);
    })
}
//验证用户编号是否存在
const SearchUid = (Uid:string)=>{
    axios({
        method:'get',
        url:'http://localhost:63517/api/User/SearchUid',
        params:{
            Uid:Uid
        }
    })
    .then(res=>{
        //console.log(res.data);
        if(res.data>0){
            //用户完善过个人信息 跳转到主页面
            //alert('即将跳转到主页面');
            router.push('/UserInvestRedeem');
        }
        else {
            //用户未完善个人信息 跳转到完善个人信息页面
            alert('即将跳转到用户详细信息注册页面');
            router.push({
                name:'UserInfo',
                params:{
                    U_id:Uid
                }

            })
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

</script>

